package com.cskaoyan.controller.stage;

import com.cskaoyan.model.OrderGoods;
import com.cskaoyan.model.bo.WxCommentBO;
import com.cskaoyan.model.bo.WxOrderBO;
import com.cskaoyan.model.bo.WxOrderCommentBO;
import com.cskaoyan.model.bo.WxSubmitBO;
import com.cskaoyan.model.vo.*;
import com.cskaoyan.service.OrderService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @description：订单模块
 * @author     ：huzhouming
 * @date       ：2022/5/9 9:28
 */
@RestController
@RequestMapping("wx/order")
public class WxOrderController {
    @Autowired
    OrderService orderService;

    @GetMapping("list")
    /**
     * @author: huzhouming
     * @description: 显示订单页面
     * @date: 2022/5/9
     * @param wxOrderBO
     * @return com.cskaoyan.model.vo.BaseRespVO
     */
    public ExceptionVO list(WxOrderBO wxOrderBO){
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()) {
            return ExceptionVO.error(501, "请先登录");
        }
       DataVO wxOrderListVO = orderService.queryWxOrderList(wxOrderBO);
       return BaseRespVO.ok(wxOrderListVO);
    }

    @GetMapping("detail")
    /**
     * @author: huzhouming
     * @description: 进入订单细节
     * @date: 2022/5/9
     * @param orderId
     * @return com.cskaoyan.model.vo.BaseRespVO
     */
    public ExceptionVO detail(Integer orderId){
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()) {
            return ExceptionVO.error(501, "请先登录");
        }
        OrderDetialVO orderDetialVO = orderService.selectDetail(orderId);
        return BaseRespVO.ok(orderDetialVO);
    }

    @PostMapping("refund")
    /**
     * @author: huzhouming
     * @description: 用户申请退款
     * @date: 2022/5/9
     * @param map
     * @return com.cskaoyan.model.vo.BaseRespVO
     */
    public ExceptionVO refund(@RequestBody Map map){
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()) {
            return ExceptionVO.error(501, "请先登录");
        }
        orderService.updateUserRefund(map);
        return BaseRespVO.ok(null);
    }

    @PostMapping("cancel")
    /**
     * @author: huzhouming
     * @description: 用户取消订单
     * @date: 2022/5/9
     * @param map
     * @return com.cskaoyan.model.vo.BaseRespVO
     */
    public ExceptionVO cancel(@RequestBody Map map){
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()) {
            return ExceptionVO.error(501, "请先登录");
        }
        orderService.updateUserCancel(map);
        return BaseRespVO.ok(null);
    }

    @PostMapping("delete")
    /**
     * @author: huzhouming
     * @description: 用户删除订单
     * @date: 2022/5/9
     * @param map
     * @return com.cskaoyan.model.vo.BaseRespVO
     */
    public ExceptionVO delete(@RequestBody Map map){
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()) {
            return ExceptionVO.error(501, "请先登录");
        }
        orderService.updateUserDelete(map);
        return BaseRespVO.ok(null);
    }

    @PostMapping("confirm")
    /**
     * @author: huzhouming
     * @description: 用户确认收货
     * @date: 2022/5/9
     * @param map
     * @return com.cskaoyan.model.vo.BaseRespVO
     */
    public ExceptionVO confirm(@RequestBody Map map){
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()) {
            return ExceptionVO.error(501, "请先登录");
        }
        orderService.updateUserConfirm(map);
        return BaseRespVO.ok(null);
    }

    @GetMapping("goods")
    /**
     * @author: huzhouming
     * @description: 打开评价界面
     * @date: 2022/5/9
     * @param goodsId
     * @param orderId
     * @return com.cskaoyan.model.vo.BaseRespVO
     */
    public ExceptionVO goods(Integer goodsId,Integer orderId){
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()) {
            return ExceptionVO.error(501, "请先登录");
        }
        OrderGoods orderGoods = orderService.selectUserGoods(goodsId,orderId);
        return BaseRespVO.ok(orderGoods);
    }

    @PostMapping("comment")
    /**
     * @author: huzhouming
     * @description: 提交评论
     * @date: 2022/5/9
     * @param wxCommentBO
     * @return com.cskaoyan.model.vo.BaseRespVO
     */
    public ExceptionVO comment(@RequestBody WxOrderCommentBO wxOrderCommentBO){
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()) {
            return ExceptionVO.error(501, "请先登录");
        }
        orderService.updateUserComment(wxOrderCommentBO);
        return BaseRespVO.ok(null);
    }

    @PostMapping("submit")
    /**
     * @author: huzhouming
     * @description: TODO 去付款之后添加到Order订单
     * @date: 2022/5/10
     * @param
     * @return com.cskaoyan.model.vo.BaseRespVO
     */
    public ExceptionVO submit(@RequestBody WxSubmitBO wxSubmitBO){
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()) {
            return ExceptionVO.error(501, "请先登录");
        }
        Integer orderId = orderService.insertOrder(wxSubmitBO);
        WxSubmitVO wxSubmitVO = new WxSubmitVO();
        wxSubmitVO.setOrderId(orderId);
        wxSubmitVO.setGrouponLinkId(wxSubmitBO.getGrouponLinkId());
        return BaseRespVO.ok(wxSubmitVO);
    }

    @PostMapping("prepay")
    /**
     * @author: huzhouming
     * @description: TODO 支付不写（只返回）
     * @date: 2022/5/10
     * @param orderId
     * @return com.cskaoyan.model.vo.BaseRespVO
     */
    public ExceptionVO prepay(@RequestBody Map map, HttpSession session){
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()) {
            return ExceptionVO.error(501, "请先登录");
        }
        int count = orderService.updatePrepay(map,session);
        if(count == 0){
        return BaseRespVO.invalidPrepayData();}else{
            return BaseRespVO.ok(null);
        }
    }
}
